package View;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import net.miginfocom.swing.MigLayout;


public class InputPanel extends JPanel{
	
	private JPanel firstPanel;
	private JLabel equationLabel;
	private JTextField equationTextField;
	private JLabel bisectionLabel;
	private JLabel intervalLabel;
	private JComponent field;
	private JSpinner intervalFirstSpinner;
	private JLabel toLabel;
	private JSpinner intervalSecondSpinner;
	private JLabel toleranceLabel;
	private JTextField toleranceTextField;
	private JLabel maxIterationLabel;
	private JTextField maxIterationTextField;
	private JPanel secondPanel;
	private JLabel newtonLabel;
	private JLabel startLabel;
	private JTextField startTextField;
	private JLabel iterationLabel;
	private JTextField iterationTextField;
	private JPanel updatePanel;
	private JButton updateButton;
	private JLabel example;
	private JButton btnNewton;
	private JButton btnBisection;
	private JLabel newtonThres;
	private JTextField newtonThresTextField;
	private JPanel panel;
	
	public InputPanel(){
		setLayout(new MigLayout());
		
		initComponent();
		addComponent();
		
	}
	
	private void initComponent(){
		
		firstPanel = new JPanel();
		firstPanel.setLayout(new MigLayout());
		
		example = new JLabel("Example: 2 3 -4 1 -5 0 means 2x3 - 4x - 5");
		panel = new JPanel();
		panel.setLayout(new MigLayout());
		
		equationLabel = new JLabel("Polynomial Equation:");
		equationTextField = new JTextField(20);
		panel.add(equationLabel);
		panel.add(equationTextField);
		
		bisectionLabel = new JLabel("Bisection Method");
		intervalLabel = new JLabel("Intervals of x:");
		
		double startMin = -1000.000;  
        double startValue = 0.000;  
        double startMax = 1000.000;  
        double startStepSize = 0.001;  
        SpinnerNumberModel startModel = new SpinnerNumberModel(startValue, startMin, startMax, startStepSize);  
        
        intervalFirstSpinner = new JSpinner(startModel);
		field = ((JSpinner.DefaultEditor) intervalFirstSpinner.getEditor());
        Dimension prefSize = field.getPreferredSize();
        prefSize = new Dimension(30, prefSize.height);
        field.setPreferredSize(prefSize);
        
        toLabel = new JLabel("to");
        
        SpinnerNumberModel secondModel = new SpinnerNumberModel(startValue, startMin, startMax, startStepSize);  
        
        intervalSecondSpinner = new JSpinner(secondModel);
        field = ((JSpinner.DefaultEditor) intervalSecondSpinner.getEditor());
        Dimension preferedSize = field.getPreferredSize();
        preferedSize = new Dimension(30, preferedSize.height);
        field.setPreferredSize(preferedSize);
        
        toleranceLabel = new JLabel("Tolerance/Threshold:");
        toleranceTextField = new JTextField(5);
        maxIterationLabel = new JLabel("Max Iteration:");
        maxIterationTextField = new JTextField(5);
        
        btnBisection = new JButton("Plot Bisection Method Graph");
				
        secondPanel = new JPanel();
        secondPanel.setLayout(new MigLayout());
        
        newtonLabel = new JLabel("Newton Method");
        startLabel = new JLabel("Starting Point:");
        startTextField = new JTextField(5);
        newtonThres = new JLabel("Threshold:");
        newtonThresTextField = new JTextField(5);
        iterationLabel = new JLabel("No. of Iteration/s:");
        iterationTextField = new JTextField(5);      

		btnNewton = new JButton("Plot Newton Method Graph");
        
        updatePanel = new JPanel();
        updatePanel.setLayout(new MigLayout());
        updateButton = new JButton("Compute");
	}
	
	private void addComponent(){
		
		add(example, "wrap 20");
		add(panel, "wrap");
		addToFirstPanel();
		add(firstPanel, "wrap");
		add(btnBisection, "gapleft 20, wrap 20");
		addToSecondPanel();
		add(secondPanel, "wrap");
		add(btnNewton, "gapleft 20, wrap 20");
		addToUpdatePanel();
		add(updatePanel);
	}

	private void addToFirstPanel(){
		firstPanel.add(bisectionLabel, "wrap");
		firstPanel.add(intervalLabel, "gapleft 20");
		firstPanel.add(intervalFirstSpinner);
		firstPanel.add(toLabel, "gapleft 20");
		firstPanel.add(intervalSecondSpinner, "gapleft 20, wrap");
		firstPanel.add(toleranceLabel, "gapx 20");
		firstPanel.add(toleranceTextField, "wrap");
		firstPanel.add(maxIterationLabel, "gapx 20");
		firstPanel.add(maxIterationTextField, "wrap");
	}

	private void addToSecondPanel() {
		secondPanel.add(newtonLabel, "wrap");
		secondPanel.add(startLabel, "gapleft 20");
		secondPanel.add(startTextField, "wrap");
		secondPanel.add(newtonThres, "gapleft 20");
		secondPanel.add(newtonThresTextField, "wrap");
		secondPanel.add(iterationLabel, "gapleft 20");
		secondPanel.add(iterationTextField, "wrap");
	}
	
	private void addToUpdatePanel(){
		updatePanel.add(updateButton);
	}
	
	public String getEquationTextField(){
		return equationTextField.getText();
	}

	public double getIntervalFirstSpinner() {
		return (double) intervalFirstSpinner.getValue();
	}

	public double getIntervalSecondSpinner() {
		return  (double) intervalSecondSpinner.getValue();
	}

	public double  getToleranceTextField() {
		return Double.parseDouble(toleranceTextField.getText());
	}

	public int getMaxIterationTextField() {
		return Integer.parseInt(maxIterationTextField.getText());
	}

	public double getStartTextField() {
		return Double.parseDouble(startTextField.getText());
	}

	public int getIterationTextField() {
		return Integer.parseInt(iterationTextField.getText());
	}
	
	public double getNewtonThresTextField(){
		return Double.parseDouble(newtonThresTextField.getText());
	}
	
	public void addUpdateListener(ActionListener listener) {
		updateButton.addActionListener(listener);
	}
	
	public void addBisctionPlotListener(ActionListener listener){
		btnBisection.addActionListener(listener);
	}
	
	public void addNewtonPlotListener(ActionListener listener){
		btnNewton.addActionListener(listener);
	}
}
